class CopyTxtPlugin {
  constructor (options) {
    const { name } = options
    console.log('运行插件', name)
  }

  apply (compiler) {
    // compiler.hooks表示调用webpack打包过程的生命周期，
    // 具体生命周期，看官网：https://www.webpackjs.com/api/compiler-hooks/

    // 同步钩子
    compiler.hooks.compile.tap('CopyTxtPlugin', (compilation) => {
      console.log('我是compile')
    })

    // 异步钩子
    compiler.hooks.emit.tapAsync('CopyTxtPlugin', (compilation, callback) => {
      console.log('我是emit')
      compilation.assets['lzc.txt'] = {
        source: function () {
          return '我是lzc.txt'
        },
        size: function () {
          return 9
        }
      }
      callback()
    })
  }
}

module.exports = CopyTxtPlugin